package com.stx.test.serialize.correct;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.FileWriter;

public class JsonClient {
    public static void main(String[] args) throws Exception {
        User user = new User("alice", 20);
        // 将对象转为JSON字符串
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(user);
        // 写入文件（模拟网络传输）
        FileWriter writer = new FileWriter("D:/shuangti/user.json");
        writer.write(json);
        writer.close();
        System.out.println("JSON数据发送：" + json);
    }
}
// 输出的JSON内容：{"username":"alice","age":20}